﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using Controller;
using Model;

public partial class ACPanel_NodeCauHinh_QLNhomLienlac : System.Web.UI.Page
{
    public static SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["defaultConnection"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGrid("NgayCapNhat", false);
        }
    }

    /// <summary>
    /// Method : FillGrid 
    /// Use : Bind data to gridview
    /// </summary>
    /// <param name="SortExpression"></param>
    /// <param name="IsSorting"></param>
    public void FillGrid(string SortExpression, bool IsSorting)
    {
        var dt = new DataTable();
        // Lay chuoi ket noi CSDL
        Connection.Open();
        var query = "SELECT * FROM [VYC-15] ORDER BY NgayCapNhat DESC";
        var dataAdapter = new SqlDataAdapter(query, Connection);
        dataAdapter.Fill(dt);
        Connection.Close(); // dong ket noi       

        var dv = dt.DefaultView;
        if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
        {
            if (IsSorting)
            {
                if (ViewState["SortExpression"].ToString() == SortExpression)
                {
                    if (ViewState["SortDirection"].ToString() == "DESC")
                        ViewState["SortDirection"] = "ASC";
                    else
                        ViewState["SortDirection"] = "DESC";
                }
                else
                {
                    ViewState["SortExpression"] = SortExpression;
                    ViewState["SortDirection"] = "DESC";
                }
            }
        }
        else
        {
            ViewState["SortExpression"] = SortExpression;
            ViewState["SortDirection"] = "DESC";
        }

        dv.Sort = ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        gv.DataSource = dv.ToTable();
        gv.DataBind();
    }

    /// <summary>
    /// Event OnPageIndexChanging="gv_PageIndexChanging" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        FillGrid(ViewState["SortExpression"].ToString(), false);
    }

    /// <summary>
    /// Event OnSorting="gv_Sorting" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        FillGrid(e.SortExpression, true);
    }

    protected void btnThemvao_OnClick(object sender, EventArgs e)
    {
        try
        {
            var controller = new VYC15Controller();
            var model = new ModelVYC15();

            // var info = (ModelVYC17)Session[Constants.SESSION_ADMIN];
            model.Ten = tbTennhomlienlac.Text;
            model.TrangThai = Convert.ToInt32(cbTrangThai.Checked);

            var isSuccess = 0;
            isSuccess = controller.AddData(model);

            if (isSuccess == 0)
            {
                // Chua thanh cong                    
                litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại.</p>";
            }
            else
            {
                litResult.Text = "<p class=\"alert alert-success\">Thêm dữ liệu thành công.</p>";
                FillGrid(ViewState["SortExpression"].ToString(), false);
                ResetInput();
            }
        }
        catch (Exception ex)
        {
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại. Message : " + ex.Message + "</p>";
            throw;
        }
    }

    public void ResetInput()
    {
        tbTennhomlienlac.Text = "";
        cbTrangThai.Checked = false;
    }

    /// <summary>
    /// Edit row
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        FillGrid("NgayCapNhat", false);
        gv.EditIndex = e.NewEditIndex;        
        gv.DataBind();        
    }

    /// <summary>
    /// Cancel edit
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv.EditIndex = -1;
        gv.DataBind();
        FillGrid("NgayCapNhat", false);
    }

    /// <summary>
    /// Update row
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var row = (GridViewRow) gv.Rows[e.RowIndex];

        var lbId = (Label) row.FindControl("lbId");
        var tbTen = (TextBox) row.FindControl("tbTen");
        var cbTrangthai = (CheckBox) row.FindControl("cbTrangthai");

        gv.EditIndex = -1;
        // update Data
        var controller = new VYC15Controller();
        var model = new ModelVYC15();
        model.Id = Convert.ToInt32(lbId.Text);
        model.Ten = tbTen.Text;
        model.TrangThai = Convert.ToInt32(cbTrangthai.Checked);

        litResult.Text = controller.EditData(model) > 0 ? "<p class=\"alert alert-success\">Cập nhật dữ liệu thành công.</p>": "<p class=\"alert alert-danger\">Lỗi! Cập nhật dữ liệu thất bại.</p>";

        FillGrid("NgayCapNhat", false);
    }
}